| Conditions | 1 |
| Paths | 1 |
| Total Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 16 | function mechanics (state, visibility, data) { |
||
| 17 | let ct = this; |
||
| 18 | ct.state = state; |
||
| 19 | ct.data = data; |
||
| 20 | |||
| 21 | ct.unlockMechanic = function(player, mechanic) { |
||
| 22 | if(ct.unlockedMechanics(player) >= player.mechanic_slots){ |
||
| 23 | return; |
||
| 24 | } |
||
| 25 | player.mechanics[mechanic] = true; |
||
| 26 | }; |
||
| 27 | |||
| 28 | ct.unlockedMechanics = function(player) { |
||
| 29 | let count = 0; |
||
| 30 | for(let key in player.mechanics){ |
||
| 31 | if(player.mechanics[key]){ |
||
| 32 | count++; |
||
| 33 | } |
||
| 34 | } |
||
| 35 | return count; |
||
| 36 | }; |
||
| 37 | |||
| 38 | /* Global upgrades are non-resource specific, repeatable upgrades */ |
||
| 39 | ct.buyGlobalUpgrade = function (name) { |
||
| 40 | if (!ct.canBuyGlobalUpgrade(name)) { |
||
| 41 | return; |
||
| 42 | } |
||
| 43 | |||
| 44 | let up = data.global_upgrades[name]; |
||
| 45 | for (let currency in up.price) { |
||
| 46 | let value = up.price[currency] * ct.priceMultiplier(name); |
||
| 47 | state.player.resources[currency].number -= value; |
||
| 48 | } |
||
| 49 | |||
| 50 | state.player.global_upgrades[name]++; |
||
| 51 | }; |
||
| 52 | |||
| 53 | ct.canBuyGlobalUpgrade = function (name) { |
||
| 54 | let up = data.global_upgrades[name]; |
||
| 55 | for (let currency in up.price) { |
||
| 56 | let value = up.price[currency] * ct.priceMultiplier(name); |
||
| 57 | if (state.player.resources[currency].number < value) { |
||
| 58 | return false; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | return true; |
||
| 63 | }; |
||
| 64 | |||
| 65 | ct.priceMultiplier = function (name) { |
||
| 66 | let level = state.player.global_upgrades[name]; |
||
| 67 | return Math.ceil(Math.pow(data.global_upgrades[name].price_exp, level)); |
||
| 68 | }; |
||
| 69 | |||
| 70 | ct.visibleMechanicUpgrades = function() { |
||
| 71 | return visibility.visible(data.global_upgrades, isMechanicUpgradeVisible); |
||
| 72 | }; |
||
| 73 | |||
| 74 | function isMechanicUpgradeVisible(name) { |
||
| 75 | let mechanic = data.global_upgrades[name].mechanic; |
||
| 76 | return state.player.mechanics[mechanic]; |
||
| 77 | } |
||
| 78 | } |
||
| 79 |